package com.company.user.vo;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.io.Serializable;

/**
 * 接口返回统一格式
 */
public class GeneralResult<T> implements Serializable {

    private boolean success;
    private String errorCode;
    private String message;
    private T data;
    private String traceId;

    public GeneralResult() {
    }

    private GeneralResult(boolean success, T data, String message, String errorCode) {
        this.success = success;
        this.data = data;
        this.message = message;
        this.errorCode = errorCode;
    }


    // ======================= 成功 ==============================
    public static <T> GeneralResult<T> genSuccessResult(T data) {
        return genResult(true, data, null, null);
    }

    public static <T>  GeneralResult<Page<T>> genSuccessResult(Page<T> data){
        if(data==null){
            data= new Page<>();
        }
        return genResult(true, data, null, null);
    }

    public static <T>  GeneralResult<IPage<T>> genSuccessResult(IPage<T> data){
        if(data==null){
            data= new Page<>();
        }
        return genResult(true, data, null, null);
    }


    public static <T> GeneralResult<T> genSuccessResult() {
        return genResult(true, null, null, null);
    }

    // ======================= 失败 ==============================
    public static <T> GeneralResult<T> genErrorResult(String message) {
        return genResult(false, null, message, null);
    }

    public static <T> GeneralResult<T> genErrorResult(String message, String errorCode) {
        return genResult(false, null, message, errorCode);
    }

    // ======================= 总 ==============================
    public static <T> GeneralResult<T> genErrorResult(String message, String errorCode, String traceId) {
        GeneralResult<T> result = genResult(false, null, message, errorCode);
        result.setTraceId(traceId);
        return result;
    }

    public static <T> GeneralResult<T> genResult(boolean success, T data, String message) {
        return genResult(success, data, message, null);
    }

    public static <T> GeneralResult<T> genResult(boolean success, T data, String message, String errorCode) {
        return new GeneralResult<>(success, data, message, errorCode);
    }


    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getTraceId() {
        return traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }
}
